package com.video.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.video.demo.domain.Camera;
import com.video.demo.entity.InterfaceUrl;
import com.video.demo.entity.ToCamera;
import com.video.demo.mapper.CameraMapper;
import com.video.demo.service.CameraService;
import com.video.demo.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;


import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CameraServiceImpl implements CameraService {
    @Autowired
    private  CameraMapper cameraMapper;
    @Autowired
    private RestTemplate rt;
    @Autowired
    private InterfaceUrl il;
    @Autowired
   private  CameraService cs;

    public CameraServiceImpl(CameraMapper cameraMapper) {
        this.cameraMapper=cameraMapper;
    }

    @Override
    public Map<String,String> ysy(String id, String deviceSerial) {
        Map map=cameraMapper.selectysy(id,deviceSerial);
        URL url = null;
        String rtmpHd = "";
        String HLS = "";
        try {
            url = new URL("https://open.ys7.com/api/lapp/token/get?appKey="+map.get("appKey")+"&appSecret="+map.get("appSecret"));
            String post = HttpUtils.post(url, map.toString());

            Map parse = (Map)JSON.parse(post);
            Map data = (Map)parse.get("data");

            Object channelNo = map.get("channel_number");

            URL url1 = new URL("https://open.ys7.com/api/lapp/live/address/limited?accessToken="+data.get("accessToken")+"&deviceSerial="+deviceSerial+"&channelNo="+channelNo);
            String post2 = HttpUtils.post(url1, "");
            Map parse2 = (Map)JSON.parse(post2);
            Map data2 = (Map) parse2.get("data");
            rtmpHd = String.valueOf(data2.get("rtmp"));
            HLS =String.valueOf(data2.get("hdAddress"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HashMap<String,String> map1 = new HashMap();
        map1.put("rtmpHd",rtmpHd);
        map1.put("HLS",HLS);

        return map1;
    }

    @Override
    public Map<String,String> tunnelysy(String id, String deviceSerial) {
        Map map=cameraMapper.tunnelysy(id,deviceSerial);
        URL url = null;
        String rtmpHd = "";
        String HLS = "";
        try {
            url = new URL("https://open.ys7.com/api/lapp/token/get?appKey="+map.get("appKey")+"&appSecret="+map.get("appSecret"));
            String post = HttpUtils.post(url, map.toString());

            Map parse = (Map)JSON.parse(post);
            Map data = (Map)parse.get("data");

            Object channelNo = map.get("channel_number");

            URL url1 = new URL("https://open.ys7.com/api/lapp/live/address/limited?accessToken="+data.get("accessToken")+"&deviceSerial="+deviceSerial+"&channelNo="+channelNo);
            String post2 = HttpUtils.post(url1, "");
            Map parse2 = (Map)JSON.parse(post2);
            Map data2 = (Map) parse2.get("data");
            rtmpHd = String.valueOf(data2.get("rtmp"));
            HLS =String.valueOf(data2.get("hdAddress"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HashMap<String,String> map1 = new HashMap();
        map1.put("rtmpHd",rtmpHd);
        map1.put("HLS",HLS);

        return map1;
    }
    @Override
    public String slect(String addressName) {
        Camera camera = new Camera();
        camera.setId(addressName);
        List<Camera> select = cameraMapper.select(camera);
        StringBuffer url = new StringBuffer();
        if (select.size()>0){
            Camera camera1 = select.get(0);
//            拿到具体信息开始拼接储存位置
            if (camera1.getProvince()!=null &&camera1.getProvince()!=""){
                url.append(camera1.getProvince()+"/");
            }
            if (camera1.getCity1()!=null && camera1.getCity1()!=""){
                url.append(camera1.getCity1()+"/");
            }
            if (camera1.getCity2()!=null && camera1.getCity2()!=""){
                url.append(camera1.getCity2()+"/");
            }
            url.append(camera1.getName());
            //      河南/南阳市宛城区/中交国通母体试验室
        }
        return url.toString();
    }

    @Override
    public String GostCamera(String appKey, String appSecret) {
        //请求头部
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        //请求体
        MultiValueMap jsonObject=new LinkedMultiValueMap();
        jsonObject.add("appKey",appKey);
        jsonObject.add("appSecret",appSecret);
//(il.getREQUEST_URL()+appKey+appSecret,String.class)

        HttpEntity entity=new HttpEntity(jsonObject,headers);
        ResponseEntity<String> responseEntity= rt.exchange(il.getREQUEST_URL()+"?"+"appKey="+appKey+"&appSecret="+appSecret, HttpMethod.POST,entity,String.class);
        String boby = responseEntity.getBody();

        //数据转化
//        ObjectMapper objectMapper = new ObjectMapper();
//     ToCamera toCamera = new ToCamera();
//        try {
//            toCamera=objectMapper.readValue(boby,ToCamera.class);
//        }catch (JsonProcessingException e){
//            e.printStackTrace();
//        }
//        String s = JSONObject.toJSONString(boby);



        JSONObject jsonObje = JSONObject.parseObject(boby);//将该字符串解析成jsonObject
        String token = jsonObje.getJSONObject("data").getString("accessToken");//提取该对象中的data域，
        ResponseEntity<String> responseEntity1= rt.exchange(il.getVIDO_URL()+"?"+"accessToken="+token,HttpMethod.POST,entity,String.class);
        String boby1 = responseEntity1.getBody();
        return boby1;
    }
}
